package com.smedi.ismedi.dangeroussourcemanage.core.port.adapter.persistence.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smedi.ismedi.dangeroussourcemanage.core.domain.aggregatemodel.DangerousSourceLibrary;
import com.smedi.ismedi.dangeroussourcemanage.core.domain.aggregatemodel.QueryDangerousSourceLibraryList;
import com.smedi.ismedi.dangeroussourcemanage.core.domain.repository.DangerousSourceLibraryRepository;
import com.smedi.ismedi.dangeroussourcemanage.core.port.adapter.persistence.model.DangerousSourceLibraryEntity;
import com.smedi.ismedi.dangeroussourcemanage.core.port.adapter.persistence.mybatis.mapper.DangerousSourceLibraryMapper;
import com.smedi.ismedi.web.starter.util.DozerUtil;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 基础信息仓库实现类
 *
 * @author xienannan
 * @date 2021/08/23
 */
@Repository
@AllArgsConstructor
public class DangerousSourceLibraryRepositoryImpl extends ServiceImpl<DangerousSourceLibraryMapper, DangerousSourceLibraryEntity> implements DangerousSourceLibraryRepository, IService<DangerousSourceLibraryEntity> {

    private final DozerUtil dozerUtil;

    @Override
    public DangerousSourceLibrary selectDangerousSourceLibraryById(Long queryDangerousSourceLibraryId) {
        DangerousSourceLibraryEntity dangerousSourceLibraryEntity = this.getBaseMapper().selectById(queryDangerousSourceLibraryId);

        return dozerUtil.convertor(dangerousSourceLibraryEntity, DangerousSourceLibrary.class);
    }

    @Override
    public List<Long> selectDangerousSourceLibraryByEngineeringCategory(String engineeringCategory) {
        List<Long> longList = new ArrayList<>();
        QueryWrapper<DangerousSourceLibraryEntity> wrapper = new QueryWrapper<>();
        if (engineeringCategory != null) {
            wrapper.eq("engineering_category", engineeringCategory);
        }
        List<DangerousSourceLibraryEntity> dangerousSourceLibraryEntityPage = this.getBaseMapper().selectList(wrapper);
        for (DangerousSourceLibraryEntity dangerousSourceLibraryEntity : dangerousSourceLibraryEntityPage) {
            longList.add(dangerousSourceLibraryEntity.getId());
        }
        return longList;
    }

    @Override
    public List<Long> selectDangerousSourceLibraryByEngineeringCategoryAndKeyHazards(String engineeringCategoryAndKeyHazards) {
        List<Long> longList = new ArrayList<>();
        QueryWrapper<DangerousSourceLibraryEntity> wrapper = new QueryWrapper<>();
        if (engineeringCategoryAndKeyHazards != null) {
            wrapper.like("key_hazards", engineeringCategoryAndKeyHazards);
            wrapper.or().like("engineering_category", engineeringCategoryAndKeyHazards);
        }
        List<DangerousSourceLibraryEntity> dangerousSourceLibraryEntityPage = this.getBaseMapper().selectList(wrapper);
        for (DangerousSourceLibraryEntity dangerousSourceLibraryEntity : dangerousSourceLibraryEntityPage) {
            longList.add(dangerousSourceLibraryEntity.getId());
        }
        return longList;
    }

    @Override
    public List<Long> selectDangerousSourceLibraryByKeyHazards(String keyHazards) {
        List<Long> longList = new ArrayList<>();
        QueryWrapper<DangerousSourceLibraryEntity> wrapper = new QueryWrapper<>();
        if (keyHazards != null) {
            wrapper.like("key_hazards", keyHazards);
        }
        List<DangerousSourceLibraryEntity> dangerousSourceLibraryEntityPage = this.getBaseMapper().selectList(wrapper);
        for (DangerousSourceLibraryEntity dangerousSourceLibraryEntity : dangerousSourceLibraryEntityPage) {
            longList.add(dangerousSourceLibraryEntity.getId());
        }
        return longList;
    }

    @Override
    public Page<DangerousSourceLibrary> selectDangerousSourceLibraryPageList(QueryDangerousSourceLibraryList queryDangerousSourceLibraryList) {
        Page<DangerousSourceLibraryEntity> dangerousSourceLibraryEntityPage = new Page<>(queryDangerousSourceLibraryList.getPageNum(), queryDangerousSourceLibraryList.getPageSize());
        Page<DangerousSourceLibrary> dangerousSourceLibraryPage = new Page<>(queryDangerousSourceLibraryList.getPageNum(), queryDangerousSourceLibraryList.getPageSize());
        dangerousSourceLibraryEntityPage.addOrder(OrderItem.desc("id"));
        QueryWrapper<DangerousSourceLibraryEntity> wrapper = new QueryWrapper<>();

        if (queryDangerousSourceLibraryList.getEngineeringCategory() != null) {
            wrapper.or()
                    .eq("engineering_category", queryDangerousSourceLibraryList.getEngineeringCategory());
        }
        if (queryDangerousSourceLibraryList.getConstructionPhase() != null) {
            wrapper.or()
                    .eq("construction_phase", queryDangerousSourceLibraryList.getConstructionPhase());
        }

        Page<DangerousSourceLibraryEntity> dangerousSourceLibraryEntityPageReturn = this.getBaseMapper().selectPage(dangerousSourceLibraryEntityPage, wrapper);
        if (dangerousSourceLibraryEntityPageReturn == null) {
            return null;
        }

        List<DangerousSourceLibrary> dangerousSourceLibraryList = dozerUtil.convertor(dangerousSourceLibraryEntityPageReturn.getRecords(), DangerousSourceLibrary.class);

        dangerousSourceLibraryPage.setRecords(dangerousSourceLibraryList);
        dangerousSourceLibraryPage.setTotal(dangerousSourceLibraryEntityPageReturn.getTotal());

        return dangerousSourceLibraryPage;
    }

    @Override
    public List<DangerousSourceLibrary> selectDangerousSourceLibraryList(QueryDangerousSourceLibraryList queryDangerousSourceLibraryList) {
        List<DangerousSourceLibraryEntity> dangerousSourceLibraryEntityList = new ArrayList<>();
        QueryWrapper<DangerousSourceLibraryEntity> wrapper = new QueryWrapper<>();

        if (queryDangerousSourceLibraryList.getEngineeringCategory() != null) {
            wrapper.or()
                    .eq("engineering_category", queryDangerousSourceLibraryList.getEngineeringCategory());
        }
        if (queryDangerousSourceLibraryList.getConstructionPhase() != null) {
            wrapper.or()
                    .eq("construction_phase", queryDangerousSourceLibraryList.getConstructionPhase());
        }

        dangerousSourceLibraryEntityList = this.getBaseMapper().selectList(wrapper);
        if (dangerousSourceLibraryEntityList == null) {
            return null;
        }

        List<DangerousSourceLibrary> dangerousSourceLibraryList = dozerUtil.convertor(dangerousSourceLibraryEntityList, DangerousSourceLibrary.class);

        return dangerousSourceLibraryList;
    }

    @Override
    public Map<Long, String> getIdNameMap(List<Long> hazardIdList) {
        LambdaQueryWrapper<DangerousSourceLibraryEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(ObjectUtils.isNotNull(hazardIdList),DangerousSourceLibraryEntity::getId,hazardIdList);
        List<DangerousSourceLibraryEntity>results = this.getBaseMapper().selectList(lambdaQueryWrapper);
        Map<Long,String>map = new HashMap<>();
        if (results != null){
            map = results.stream().collect(Collectors.toMap(DangerousSourceLibraryEntity::getId,DangerousSourceLibraryEntity::getKeyHazards));
        }
            return map;
    }

}